// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html


import ComponentBase from "../../BASE/ComponentBase";
import GeZiManager from "../../Manager/GeZiManager";
import KHD from "../../Message/KHD";
import KHD2 from "../../Message/KHD2";
import { Message } from "../../Message/Message";
import MessageCenter from "../../Message/MessageCenter";
import { SHMessage } from "../../Message/SHMessage";
import S03 from "../../Scharacter/S03";
import SXX from "../../Scharacter/SXX";
import shopUI from "../../UIS/shop/shopUI";
import A509 from "../../equipment/weapon/SC/A509";
import weapon from "../../equipment/weapon/weapon";
import turn from "../../game/time/turn";
import NWshopM from "./NWshopM";

import shops from "./shops";
import Shops from "./shops";




const {ccclass, property} = cc._decorator;

@ccclass
export default class shopM extends ComponentBase {
    static SL:number=1;//武器更新次数
 static   A1n:any[]=[2,0,"A110"];//价格，数量
 static   A2n:any[]=[2,0,"A210"];
 static   A3n:any[]=[2,0,"A310"];
  static  A4n:string=null;

  static  A5n:string=null;

 
  static  A6n:number[]=[2,1];//地雷
  static  A7n:number[]=[2,1,1];//旗帜
   
  static n:number=1;//1是普通商店。2是女巫商店
    static shopUI:cc.Node=null;
static AAp1: string[] = [
    "A1422",  "A121", "A122","A228","A221","A321","A323"]
    static Apool1: string[] = [
      "B03","B07","B08", "B10", "B12","B01" ,"C01","C01","C02","C06","C07","C14","C15","D01","D04","D08","D12","D11","D13"
      ];
      static AAp2:string[] = [
        "A141","A142", "A143","A242","A243","A345"]
      static Apool2:string[] = [
       "B15","B16","B17","B13","B09","D06","D10","D16-b","D15","C10","C08","C15"
      ];
      static AAp3: string[] = [
        "A362", "A363", "A461", "A462"]
      static Apool3: string[] = [
        "B11", "B14","B17", "D06","C12","B14","D20"
      ];





      static z3: string[] = ["TNS","qi","HP","Hun"]
     static A1: string[] = [
       "A121","A122","A125","A221","A227","A228","A321","A323"];
        static A2: string[] = [
            "A141","A142", "A144","A145","A223","A242","A243","A345"]; 
            static A_2: string[] = [
             "A1421","A1422", "A110","A127", "A227","A225","A321","A210","A322"];
           
            static A_3: string[] = [
               "A110","A128","A125","A228","A225","A224","A322","A321","A223","A143"];
                static A_4: string[] = [
                    "A143",  "A142","A145","A147","A223","A242","A243","A345","A322P"]; 
      static B_3: string[] = ["B01", "B03",  "B13","B15", "B02", "B12","B19"];
      static B_4: string[] = ["B02P", "B04", "B11","B18"];
      static B1: string[] = ["B01", "B06", "B07", "B08", "B05","B10", "B12"];
      static B2: string[] = [ "B16", "B11","B17", "B18"];
      static C_3: string[] = ["C03", "C08", "C09", "C11", "C10","C01","C18"];
      static C_4: string[] = ["C12", "C19","C13","C20"];
      static C1: string[] = ["C02", "C01", "C04", "C06", "C07", "C14","C15","C17"];
      static C2: string[] = ["C15", "C13", "C18","C19"];
      static D_3: string[] = ["D02", "D07", "D10", "D16","D05", "D18","D06"];
      static D_4: string[] = ["D09","D13","D15", "D17","D21"];
      static D1: string[] = ["D01", "D04", "D05", "D08", "D11", "D12", "D14","D03"];
      static D2: string[] = ["D15", "D17", "D13","D06"];
      static Z_5: string[] = ["B01P", "B09","B14", "A261", "A164", "A363", "A362", "A1461","D19", "D20", "C16"];
      


static  TGEQ:string[]=[]

   
////////////////////////////////////////////////////////////////
   


 
    static i:number=1//升级的两个选项
   
 static   getRA(arr:string[], n:number) {
    const len = arr.length;
    const result = [];
    
    if (n >= len) {
        return arr.slice(); // 如果要选取的元素数量大于等于数组长度，直接返回整个数组的副本
    }
    
    const pickedIndexes = new Set();
    while (pickedIndexes.size < n) {
        let randomIndex = Math.floor(Math.random() * len);
        pickedIndexes.add(randomIndex);
    }
    
    pickedIndexes.forEach((index: number) => {
        result.push(arr[index]);
    });
    
    return result;
}
static AStrings(stringArray:number, n: number, k: number): string[] {
    // 过滤出以"A"开头的字符串
let A1 ,A2,B,C,D

switch (shopM.SL) {
    case 1:C=shopM.getRA(shopM.C1,1)[0];D=shopM.getRA(shopM.D1,1)[0];B="A310";A1="A110";A2="A210";
        
        break;
        case 2:C=shopM.getRA(shopM.C1,1)[0];D=shopM.getRA(shopM.D1,1)[0];B=shopM.getRA(shopM.B1,1)[0];let AA=shopM.getRA(shopM.A1,2);A2=AA[0];A1=AA[1]
        
        break;
     
    default:C=shopM.getRA(shopM.C2,1)[0];D=shopM.getRA(shopM.D2,1)[0];B=shopM.getRA(shopM.B2,1)[0];let AA1=shopM.getRA(shopM.A2,2);A2=AA1[0];A1=AA1[1]
        break;
}

console.log([C,D,B,A1,A2])
return [A1,A2,B,C,D]
   
}

   onLoad(){
        turn.Doo.push(this);
        turn.Roo.push(this);
        shopM.shopUI=cc.find("Canvas/UIManager/shopUI")
      MessageCenter.addReceive(this)
        //shopM.BTshop();
    }
    
    
static getTGEQ(){
if (KHD2.PT.includes(6)) {
    
}


}
    
 roundPass(){
    if (turn.round%12==1) {
        shopM.ToBT()
    }    

      if ((turn.round % 12 === 4) || (turn.round  % 12 === 1) ){
    
        shopM.BTshop();
      
      
      }else{ if(turn.round%6==1){

        shopM.ToNW()
            }}
   
          

            
 }


 static ToNW(){
    cc.find("Canvas/UIManager/BUIManager/BUI/BaseChoose/shop").active=false;
    cc.find("Canvas/UIManager/BUIManager/BUI/BaseChoose/NWshop").active=true;
NWshopM.GSNWshop();
console.log(cc.find("Canvas/UIManager/BUIManager/BUI/BaseChoose/NWshop").active)






}
static ToBT(){ 
    
  
   
    shopM.A1n[2]=[]
    shopM.A2n[2]=[]
    shopM.A3n[2]=[]
    cc.find("Canvas/UIManager/BUIManager/BUI/BaseChoose/shop").active=true;
cc.find("Canvas/UIManager/BUIManager/BUI/BaseChoose/NWshop").active=false;
console.log(cc.find("Canvas/UIManager/BUIManager/BUI/BaseChoose/NWshop").active)
//NWshopM.PMend();

}
























ReceiveMessage(message: Message): void {
   let k=1
   
    if (message instanceof SHMessage&&message.Type=="SU") {
   
        switch (message.SHtype) {
            case "FTP":  
                break;
            case "pool": shopM.A1n=message.Content[0];shopM.A2n=message.Content[1];shopM.A3n=message.Content[2];  shopM.A4n=message.Content[3];shopM.A5n=message.Content[4];shopM.A6n=message.Content[5];
            shopM.TGEQ=message.Content[6]
            console.log(shopM.TGEQ)
            shopM.shopUI.getComponent(shopUI).ST();
                break;
            case "buy": 
            let a   ;let b;
            switch (message.Content) {
                case 1:a=shopM.A1n[2];if(this.PPC()){}else{for(let m of GeZiManager.PCP.shop2){m.Shop1();}}shopM.A1n[1]-=1; for(let m of GeZiManager.PCP.shop2){m.Shop2(a,false);};b=shopM.A1n[0]+GeZiManager.free;shopM.A1n[2]=null
                    //
                    break;
                    case 2:a=shopM.A2n[2];if(this.PPC()){}else{for(let m of GeZiManager.PCP.shop2){m.Shop1();}}shopM.A2n[1]-=1; for(let m of GeZiManager.PCP.shop2){m.Shop2(a,false);};b=shopM.A2n[0]+GeZiManager.free;shopM.A2n[2]=null
                    
                    break;
                    case 3:a=shopM.A3n[2];if(this.PPC()){}else{for(let m of GeZiManager.PCP.shop2){m.Shop1();}}shopM.A3n[1]-=1; for(let m of GeZiManager.PCP.shop2){m.Shop2(a,false);};b=shopM.A3n[0]+GeZiManager.free;shopM.A3n[2]=null
                    
                    break;
                    case 4:a=shopM.A4n;shopM.A4n=null;if(this.PPC()){}else{for(let m of GeZiManager.PCP.shop2){m.Shop1();}}; for(let m of GeZiManager.PCP.shop2){m.Shop2(a,false);};b=k+turn.day+GeZiManager.free;
                 
                    break;
                    case 5:a=shopM.A5n;shopM.A5n=null;if(this.PPC()){}else{for(let m of GeZiManager.PCP.shop2){m.Shop1();}}; for(let m of GeZiManager.PCP.shop2){m.Shop2(a,false);};b=k+turn.day+GeZiManager.free;
                  
                    break;
                default:;a=this.levelup(message.Content-5)[0];b=this.levelup(message.Content-5)[1];for(let m of GeZiManager.PCP.shop2){m.Shop2(a,true);}
                    break;
            }
            if (a!=null) {
                MessageCenter.MakeSHMessage("AM",[GeZiManager.PCP.ZB],a,GeZiManager.PCP.Pturn,"changeE")
            }
          
            MessageCenter.MakeSHMessage("AM",[GeZiManager.PCP.ZB],GeZiManager.PCP.TNJJ[5]+GeZiManager.TNC[5],GeZiManager.PCP.Pturn,"TN-")
            MessageCenter.MakeSHMessage("AM",[GeZiManager.PCP.ZB],b,GeZiManager.PCP.Pturn,"Qi-")
            GeZiManager.TNC[5]=2;
   GeZiManager.PCP.Bu.push(5);
                break;
        
            default:
                break;
        }
        for(let m of GeZiManager.PCP.shop3){m.Shop3();}
    }
}
levelup(i:number){ 
    
    let A1=null
    let co
    //GeZiManager.PCP.weapon.getComponent(weapon).TheNumber
    switch (GeZiManager.PCP.weapon.TheNumber) {
    
    
    case "A110": switch(i){
        case 1:A1="A131"
         break;
        case 2:A1="A132"
        break;
     }
     co=2;
        break;
 case "A131": A1="A151" ; co=4;
        break;
 case "A132": A1="A152" ; co=4;
        break;
    
    
        case "A210": switch(i){
            case 1:A1="A231"
             break;
            case 2:A1="A232"
            break;
         }
         co=3;
            break;
     case "A231": A1="A251" ; co=3;
            break;
     case "A232": A1="A252" ; co=3;
            break;
        

            case "A310": switch(i){
                case 1:A1="A331";   co=3;
                 break;
                case 2:A1="A332";co=2;
                break;
             }
          
                break;
         case "A331": A1="A351" ; co=2;
                break;
         case "A332": A1="A352" ; co=3;
                break;
          case "A580":switch(i){
            case 1:A1="A581";co=2;
             break;
            case 2:A1="A582";co=2;
            break;
         }
         
break;

case "A121": A1="A146" ; co=5;
break
case "A122": A1="A144" ; co=4;
break
case "A127": A1="A142" ; co=5;
break
case "A125": A1="A141" ; co=4;
break
case "A126": A1="A145" ; co=3;

break
case "A128": A1="A147" ; co=3;
break
case "A141": A1="A161" ; co=5;
break
case "A142": A1="A162" ; co=6;
break
case "A221": A1="A221P" ; co=2;
break
case "A223": A1="A262" ; co=6;
break
case "A228": A1="A242" ; co=3;
break


case "A223": A1="A262" ; co=6;
break
case "A242": A1="A263" ; co=5;
break
case "A321": A1="A321P" ; co=3;
break;
case "A321P": A1="A352" ; co=4;
break;
case "A322": A1="A342" ; co=3;
break;
case "A345": A1="A361" ; co=6;
break


case "A581":switch(i){
    case 1:A1="A581P1";co=4;
     break;
    case 2:A1="A581P2";co=4;
    break;
 }
 
break;
case "A582":switch(i){
    case 1:A1="A582P1";co=3;
     break;
    case 2:A1="A582P2";co=3;
    break;
 }
 
break;


case "A583": A1="A583P" ; co=3;
break;
case "A503":switch(i){
    case 1:A1="A503A";
     break;
    case 2:A1="A503B";
    break;
 }co=2;
break;


case "A509":
    
   A1="A509"
    co=4;
break;

case "A504":
    
   A1="A110"
    co=2;
break;





        default:
        break;
}
return [A1,co];
}







start(): void {
    shopM.BTshop()
}









static BTshop(){
    


    if (KHD2.PT.includes(6)||MessageCenter.Text) {
      let j=5;
      let k=0
      let g=["S01","S02","S04","S07","S10"]
      let o=shopM.AStrings(turn.day,k,j)
      //for(let j of [GeZiManager.P1,GeZiManager.P2,GeZiManager.P3,GeZiManager.P4,GeZiManager.P5,GeZiManager.P6]){

     //   if(g.includes(j.node.getComponent(SXX).Snumber)){k++}
      
      
     // }
     console.log(o);
     // if(k<2){k=0}else{k-=2}
      //  if(turn.round<3){ j=2
        //    k=0}
    
         
      if (turn.round>=3) {
          shopM.A1n[2]=o[0]
          shopM.A2n[2]=o[1]
          shopM.A3n[2]=o[2]
       }
       shopM.A4n=o[3]
       shopM.A5n=o[4]
      
      if(shopM.A1n[1]<2){shopM.A1n[1]+=1};
    if(shopM.A2n[1]<2){shopM.A2n[1]+=1};
    if(shopM.A3n[1]<2){shopM.A3n[1]+=1};
      
    shopM.A1n[0]=turn.day*2
    shopM.A2n[0]=turn.day*2
    shopM.A3n[0]=turn.day*2
    let l=shopM.GETSM(turn.round)
   
    
    if(shopM.A6n[1]==0){shopM.A6n[0]+=1};
     if(shopM.A6n[1]!=3){shopM.A6n[1]+=1}
        MessageCenter.MakeGMessage("SU",[GeZiManager.PCP.ZB],[shopM.A1n,shopM.A2n,shopM.A3n,shopM.A4n,shopM.A5n,shopM.A6n,l],GeZiManager.PCP.Pturn,"pool")



        console.log(l)
     }
  
 
   





    if(shopM.A7n[1]<1){shopM.A7n[1]+=1};
    if(shopM.A7n[2]<1){shopM.A7n[2]+=1};
   
shopM.SL++
    }

static GETSM(n:number){
  console.log(n)
  
  
    if(n>18){
    
        return  this.mArr1Arr2(this.A_4,this.C_4,this.B_4,this.D_4)
    
    }
if(n>12){
    
    return  this.mArr1Arr2(this.A2,this.C2,this.B2,this.D2)

}

if(n>6){
    
    return  this.mArr1Arr2(this.A_3,this.C_3,this.B_3,this.D_3)

}

if(n>-1){
    
    return  this.mArr1Arr2(this.A1,this.C1,this.B1,this.D1)

}

}






static getGL(n:number){
let a
    switch (n) {
    case 1: a=[1]
        
        break;
        case 2: a=[1,2,2,2,3]
        
        break;

        case 3: a=[2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4]
        
        break;

        case 4: a=[2,3,3,3,3,3,4,4,4,5]
        
        break;

     

    default:a=[2,3,3,3,4,4,4,5,5,5]
        break;
    
}

let b;
let k
console.log(a[Math.floor(Math.random() * a.length)])
switch (n) {
    case 1:return shopM.Apool1;//k=shopM.getRA(b,1)[0];GeZiManager.shanchuALL(shopM.Apool1,k)
        
        break;
        case 2:return b=shopM.Apool2;//k=shopM.getRA(b,1)[0];GeZiManager.shanchuALL(shopM.Apool2,k)
        break;
      

    default:return b=shopM.Apool3;//k=shopM.getRA(b,1)[0];GeZiManager.shanchuALL(shopM.Apool3,k)
        break;
}

return k





}









static mArr1Arr2<T>(...arrays: T[][]): T[] {
    const mergedArray: T[] = [].concat(...arrays);
    return mergedArray;
  }


static getEQ(n:number){
let b

function shuffleArray<T>(array: T[]): T[] {
    // 创建数组的副本，以避免修改原始数组
    const result = [...array];
    let currentIndex = result.length, randomIndex, temporaryValue;

    // 当还有元素需要打乱时
    while (currentIndex !== 0) {
        // 选取一个随机的索引
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex -= 1;

        // 交换当前元素和随机选取的元素
        temporaryValue = result[currentIndex];
        result[currentIndex] = result[randomIndex];
        result[randomIndex] = temporaryValue;
    }

    return result;
}
    GeZiManager.Tcharacter=null
    MessageCenter.MakeSHMessage("AM",[n],1,-1,"getOneC")
if(GeZiManager.Tcharacter&&GeZiManager.Tcharacter.weapon.TheNumber=="AXX"){
   
     b=shopM.TGEQ.filter(item => (item.startsWith('A') ));
    

    
}else if (GeZiManager.Tcharacter&&GeZiManager.Tcharacter.shose.TheNumber=="CXX") {
    b=shopM.TGEQ.filter(item => (item.startsWith('C') ))
}else if (GeZiManager.Tcharacter&&GeZiManager.Tcharacter.accessory.TheNumber=="DXX") {
    b=shopM.TGEQ.filter(item => (item.startsWith('D') ))
}else if (GeZiManager.Tcharacter&&GeZiManager.Tcharacter.body.TheNumber=="BXX") {
    b=shopM.TGEQ.filter(item => (item.startsWith('B') ))
}else b=["FFF"]



b=shuffleArray(b)


console.log(b)
GeZiManager.shanchu(shopM.TGEQ,b[0])

for(let m of  GeZiManager.Tcharacter.shop2){m.Shop2(b,false);}
if (KHD2.PT.includes(6)||MessageCenter.Text) {
    MessageCenter.MakeGMessage("AM",[ GeZiManager.Tcharacter.ZB],b[0],GeZiManager.PCP.Pturn,"changeE")
}

}

}